package com.meteor.downloader.videodownloader.admdm

import android.content.Context
import android.os.Bundle
import com.facebook.appevents.AppEventsLogger
import com.google.android.gms.ads.AdValue
import com.google.android.gms.ads.appopen.AppOpenAd
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.nativead.NativeAd
import com.google.firebase.analytics.FirebaseAnalytics
import com.meteor.downloader.videodownloader.BuildConfig
import com.meteor.downloader.videodownloader.utils.DataManagerMdmUtils
import com.reyun.solar.engine.SolarEngineManager
import com.reyun.solar.engine.infos.SEAdImpEventModel
import java.math.BigDecimal
import java.util.Currency

object AdInfoUtils {
    val mMdmShowAdList = if (BuildConfig.DEBUG) {
        "WwogIHsKICAgICJtZG1BZElEIjogImNhLWFwcC1wdWItMzk0MDI1NjA5OTk0MjU0NC85MjU3Mzk1OTIxIiwKICAgICJtZG1PdGhlciI6ICJvdGhlciIsCiAgICAibWRtVmFsdWVMZXZlbCI6IDEwMDAsCiAgICAibWRtQWRUeXBlIjogIm1kbU9wZW4iCiAgfSwKICB7CiAgICAibWRtQWRJRCI6ICJjYS1hcHAtcHViLTM5NDAyNTYwOTk5NDI1NDQvOTI1NzM5NTkyMSIsCiAgICAibWRtT3RoZXIiOiAib3RoZXIiLAogICAgIm1kbVZhbHVlTGV2ZWwiOiA5MDAsCiAgICAibWRtQWRUeXBlIjogIm1kbU9wZW4iCiAgfSwKICB7CiAgICAibWRtQWRJRCI6ICJjYS1hcHAtcHViLTM5NDAyNTYwOTk5NDI1NDQvMTAzMzE3MzcxMiIsCiAgICAibWRtT3RoZXIiOiAib3RoZXIiLAogICAgIm1kbVZhbHVlTGV2ZWwiOiAxMDAsCiAgICAibWRtQWRUeXBlIjogIm1kbUludGVyc09wZW4iCiAgfSwKICB7CiAgICAibWRtQWRJRCI6ICJjYS1hcHAtcHViLTM5NDAyNTYwOTk5NDI1NDQvOTI1NzM5NTkyMSIsCiAgICAibWRtT3RoZXIiOiAib3RoZXIiLAogICAgIm1kbVZhbHVlTGV2ZWwiOi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TA0ODcxMzUxMTA2LzY1MTc3NDcxNjciLAogICAgIm1kbU90aGVyIjogIm90aGVyIiwKICAgICJtZG1WYWx1ZUxldmVsIjogOTAsCiAgICAibWRtQWRUeXBlIjogIm1kbU5hbWRtdmUiCiAgfSwKICB7CiAgICAibWRtQWRJRCI6ICJjYS1hcHAtcHViLTM3Mzc5MDQ4NzEzNTExMDYvNDA1NjM1NDkxMCIsCiAgICAibWRtT3RoZXIiOiAib3RoZXIiLAogICAgIm1kbVZhbHVlTGV2ZWwiOiA4MCwKICAgICJtZG1BZFR5cGUiOiAibWRtTmFtZG12ZSIKICB9LAogIHsKICAgICJtZG1BZElEIjogImNhLWFwcC1wdWItMzczNzkwNDg3MTM1MTEwNi81NDAzNDU5MTIwIiwKICAgICJtZG1PdGhlciI6ICJvdGhlciIsCiAgICAibWRtVmFsdWVMZXZlbCI6IDEwMCwKICAgICJtZG1BZFR5cGUiOiAibWRtSW50ZXJzIgogIH0sCiAgewogICAgIm1kbUFkSUQiOiAiY2EtYXBwLXB1Yi0zNzM3OTA0ODcxMzUxMTA2LzI2ODc0MzI5MjYiLAogICAgIm1kbU90aGVyIjogIm90aGVyIiwKICAgICJtZG1WYWx1ZUxldmVsIjogOTAsCiAgICAibWRtQWRUeXBlIjogIm1kbUludGVycyIKICB9LAogIHsKICAgICJtZG1BZElEIjogImNhLWFwcC1wdWItMzczNzkwNDg3MTM1MTEwNi8xMjEyOTMyNjcwIiwKICAgICJtZG1PdGhlciI6ICJvdGhlciIsCiAgICAibWRtVmFsdWVMZXZlbCI6IDgwLAogICAgIm1kbUFkVHlwZSI6ICJtZG1JbnRlcnMiCiAgfQpd"
    }
    const val MDM_SHOW_AD_LIST = "mdmShowAdList"
    private var MDM_AD_WORTH_SINGLE = "mdmADWorthSingle"
    private var MDM_AD_WORTH_MORE = "mdmADWorthMore"

    fun adValueUpload(context: Context, adValue: AdValue) {
        //facebook
        val mdmFaceLogger = AppEventsLogger.newLogger(context)
        mdmFaceLogger.logPurchase(
            BigDecimal(adValue.valueMicros / 1000000.0), Currency.getInstance("USD")
        )

        val valueMicros1000 = adValue.valueMicros.toFloat() / 1000000.0f
        FirebaseAnalytics.getInstance(context).logEvent(MDM_AD_WORTH_SINGLE, Bundle().apply {
            putString(FirebaseAnalytics.Param.CURRENCY, "USD")
            putFloat(FirebaseAnalytics.Param.VALUE, valueMicros1000)
        })
        val thisTotal = DataManagerMdmUtils.getDataKeyMdm(MDM_AD_WORTH_MORE, 0f) + valueMicros1000

        DataManagerMdmUtils.setDataKeyMdm(MDM_AD_WORTH_MORE, thisTotal)

        if (thisTotal >= 0.01f) {
            FirebaseAnalytics.getInstance(context).logEvent(MDM_AD_WORTH_MORE, Bundle().apply {
                putString(FirebaseAnalytics.Param.CURRENCY, "USD")
                putFloat(FirebaseAnalytics.Param.VALUE, thisTotal)
            })
            DataManagerMdmUtils.setDataKeyMdm(MDM_AD_WORTH_MORE, 0.0f)
        }
    }

    fun setAdSeMdmOpen(adValue: AdValue, ad: AppOpenAd) {
        val loadedAdapterResponseInfo = ad.responseInfo.loadedAdapterResponseInfo
        val adSourceName = loadedAdapterResponseInfo?.adSourceName
        val adSourceId = loadedAdapterResponseInfo?.adSourceId
        val seAdModel = SEAdImpEventModel()
        seAdModel.setAdNetworkPlatform(adSourceName)
        seAdModel.setMediationPlatform("admob")
        seAdModel.setAdType(2)
        seAdModel.setAdNetworkAppID(adSourceId)
        seAdModel.setAdNetworkADID(ad.adUnitId)
        seAdModel.setEcpm(adValue.valueMicros / 1000.0)
        seAdModel.setCurrencyType(adValue.currencyCode)
        seAdModel.setRenderSuccess(true)
        SolarEngineManager.getInstance().trackAdImpression(seAdModel)
    }

    fun setAdSeMdmInters(adValue: AdValue, ad: InterstitialAd) {
        val loadedAdapterResponseInfo = ad.responseInfo.loadedAdapterResponseInfo
        val adSourceName = loadedAdapterResponseInfo?.adSourceName
        val adSourceId = loadedAdapterResponseInfo?.adSourceId
        val seAdModel = SEAdImpEventModel()
        seAdModel.setAdNetworkPlatform(adSourceName)
        seAdModel.setMediationPlatform("admob")
        seAdModel.setAdType(3)
        seAdModel.setAdNetworkAppID(adSourceId)
        seAdModel.setAdNetworkADID(ad.adUnitId)
        seAdModel.setEcpm(adValue.valueMicros / 1000.0)
        seAdModel.setCurrencyType(adValue.currencyCode)
        seAdModel.setRenderSuccess(true)
        SolarEngineManager.getInstance().trackAdImpression(seAdModel)
    }

    fun setAdSeMdmNative(adValue: AdValue, ad: NativeAd, adId: String) {
        val loadedAdapterResponseInfo = ad.responseInfo?.loadedAdapterResponseInfo
        val adSourceName = loadedAdapterResponseInfo?.adSourceName
        val adSourceId = loadedAdapterResponseInfo?.adSourceId
        val seAdModel = SEAdImpEventModel()
        seAdModel.setAdNetworkPlatform(adSourceName)
        seAdModel.setMediationPlatform("admob")
        seAdModel.setAdType(6)
        seAdModel.setAdNetworkAppID(adSourceId)
        seAdModel.setAdNetworkADID(adId)
        seAdModel.setEcpm(adValue.valueMicros / 1000.0)
        seAdModel.setCurrencyType(adValue.currencyCode)
        seAdModel.setRenderSuccess(true)
        SolarEngineManager.getInstance().trackAdImpression(seAdModel)
    }
}